package org.feluma.sgci.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;

public class Criptografia{

  public Criptografia(){}

  private static MessageDigest md = null;

  static{
    try{
        md = MessageDigest.getInstance("MD5");
       }
    catch(NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }
  }

  private static char[] hexCodes(byte[] text) {
    char[] hexOutput = new char[text.length * 2];
    String hexString;
    for(int i = 0; i < text.length; i++) {
        hexString = "00" + Integer.toHexString(text[i]);
        hexString.toUpperCase().getChars(hexString.length() - 2,hexString.length(), hexOutput, i * 2);
    }
    return hexOutput;
  }

  public static String criptografarMD5(String texto){
    if(md != null){
        return new String(hexCodes(md.digest(texto.getBytes())));
    }
    return null;
  }
    
  public static String criptografarSHA1(String texto){
    String senha = texto;   
    MessageDigest md;
    String retornaSenha = "";
    try {
    	md = MessageDigest.getInstance( "SHA" );
		md.update( senha.getBytes() );   
		BigInteger hash = new BigInteger( 1, md.digest() ); 
		retornaSenha = hash.toString( 16 ); 
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}    
       return retornaSenha;                           
    }
    
}
